package Simcomp::FileLoader;
use strict;
use warnings;
use XML::Simple;

our $VERSION = "0.01";
our @EXPORT = qw( load_config );
our @EXPORT_OK = qw( load_config );
our %EXPORT_TAGS = ( all => [@EXPORT_OK] );

sub load_config
{
    my $xs = XML::Simple->new();
    my $config = $xs->XMLin( "./etc/conf.xml" );
    my %data;

    for my $time ( @{$config->{'tempo'}} )
    {
	my $type = $time->{'tipo'};
	my %dist_info = ( type => $type );
	if ( $type eq "exponencial" )
	{
	    if ( defined $time->{'media'} )
	    {
		$dist_info{'params'} = [($time->{'media'})];
	    }
	    else
	    {
		&quit_load;
	    }
	}
	elsif ( $type eq "constante" )
	{
	    if ( defined $time->{'valor'} )
	    {
		$dist_info{'params'} = [($time->{'valor'})];
	    }
	    else
	    {
		&quit_load;
	    }
	}
	elsif ( $type eq "normal" )
	{
	    if ( defined ($time->{'media'}) && defined( $time->{'desvio-padrao'}) )
	    {
		$dist_info{'params'} = [($time->{'media'}, $time->{'desvio-padrao'})];
	    }
	    else
	    {
		&quit_load;
	    }
	}

	$data{$time->{'nome'}} = \%dist_info;

    }

    return %data;
}

sub quit_load
{
    die "Erro na configuração do XML. Por favor cheque a configuração e rode o programa novamente";
}
